/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.ASWingUtils;
import org.aswing.tree.TreePath;

import com.bourre.data.collections.IndexedArray;
import com.bourre.data.collections.Map;
import com.bourre.data.iterator.Iterator;

import fvaswing.components.FvTree;
import fvaswing.components.tree.FvTreeBind;
import fvaswing.components.tree.FvTreeNode;
import fvaswing.utils.FvAsWingDebug;

/**
 * Bind manager for linked tree nodes defined in {@link fvaswing.components.FvTree} 
 * tree componant.
 *
 * @author Romain Ecarnot
 */
class fvaswing.components.tree.FvTreeBinder
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _map : Map = new Map();
	
		
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Registers passed-in {@code nodeID} tree node identifier included in 
	 * {@code treeID} {@link fvaswing.components.FvTree} componant with 
	 * {@code sourceID} tree node.
	 * 
	 * <p>When {@code sourceID} tree node value is update, {@code nodeID} 
	 * tree node is updated too.
	 */
	public static function bind( sourceID : String, nodeID : Number, treeID : String ) : Void
	{
		var bList : IndexedArray;
		
		if( !_map.containsKey( sourceID ) )
		{
			bList = new IndexedArray();
			_map.put( sourceID, bList );
		}
		else bList = _map.get( sourceID );
				
		bList.push( new FvTreeBind( nodeID, treeID ) );
	}
	
	/**
	 * Unregisters passed-in {@code node} from being update when conent is 
	 * updated.
	 */
	public static function unbind( node : FvTreeNode ) : Void
	{
		var sourceID : String = node.getUserObject().getIdCode();
		
		if( _map.containsKey( sourceID ) )
		{
			var bList : IndexedArray = _map.get( sourceID ); 
			var it : Iterator = bList.getIterator();
			
			while( it.hasNext() )
			{
				var bind : FvTreeBind = it.next();
				
				if( bind.getNodeIdentifier() == node.getID() ) bList.remove( bind );
			}
		}
		else FvAsWingDebug.DEBUG( 'node [ ' + node.getID() + ' ] is not registred in binder' );
	}
	
	/**
	 * Calls all registred nodes to be updated when {@code sourceID} content is updated.
	 */
	public static function notifyUpdate( sourceID : String, newValue ) : Void
	{
		if( _map.containsKey( sourceID ) )
		{
			var it : Iterator = _map.get( sourceID ).getIterator();
			
			while( it.hasNext() )
			{
				var bind : FvTreeBind = it.next();
				var tree : FvTree = FvTree( ASWingUtils.getDisplayableComponent( bind.getTreeIdentifier() ) );
				var o : FvTreeNode = tree.getNodeById( bind.getNodeIdentifier() );
				
				FvAsWingDebug.DEBUG( 'update linked in tree ' + tree.getName() );
				
				if( tree != null && o != null )
				{
					tree.getModel().valueForPathChanged( new TreePath( o.getPath() ), newValue );	
				}
				
			}
		}
	}
		
}